package team.knowers.tourist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import team.knowers.tourist.common.convention.exception.ClientException;
import team.knowers.tourist.dao.entity.TouristDO;
import team.knowers.tourist.dao.mapper.TouristMapper;
import team.knowers.tourist.dto.req.TouristLoginReqDTO;
import team.knowers.tourist.dto.req.TouristRegisterReqDTO;
import team.knowers.tourist.service.TouristService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TouristServiceImpl extends ServiceImpl<TouristMapper, TouristDO> implements TouristService {
    /**
     * 校验身份证号是否合法
     * @param idCard
     * @return
     */
    public static boolean isValidDate(String idCard) {
        if (idCard.length() != 18) {
            return false;
        }
        String birthDateStr = idCard.substring(6, 14);
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            LocalDate birthDate = LocalDate.parse(birthDateStr, formatter);
            LocalDate minDate = LocalDate.of(1900, 1, 1);
            LocalDate maxDate = LocalDate.of(2024, 12, 31);
            return !birthDate.isBefore(minDate) && !birthDate.isAfter(maxDate);
        } catch (DateTimeParseException e) {
            return false;
        }
    }
    @Override
    public void register(TouristRegisterReqDTO req) {
        if (!isValidDate(req.getIdentifyNumber())) {
            throw new ClientException("身份证号异常");
        }
        LambdaQueryWrapper<TouristDO> wrapper = Wrappers.lambdaQuery(TouristDO.class).eq(TouristDO::getIdentifyNumber, req.getIdentifyNumber()).eq(TouristDO::getDelFlag,0);
        TouristDO touristDO = baseMapper.selectOne(wrapper);
        if (touristDO != null) {
            throw new ClientException("已有账号，请前往登录");
        }
        LambdaQueryWrapper<TouristDO> wrapper1 = Wrappers.lambdaQuery(TouristDO.class).eq(TouristDO::getUsername, req.getUsername()).eq(TouristDO::getDelFlag,0);
        TouristDO touristDO1 = baseMapper.selectOne(wrapper1);
        if (touristDO1 != null) {
            throw new ClientException("账号已存在，请修改用户名");
        }

        TouristDO newTourist = new TouristDO();
        BeanUtils.copyProperties(req, newTourist);
        baseMapper.insert(newTourist);
    }
    @Override
    public TouristDO login(TouristLoginReqDTO req) {
        LambdaQueryWrapper<TouristDO> wrapper = Wrappers.lambdaQuery(TouristDO.class).eq(TouristDO::getUsername, req.getUsername()).eq(TouristDO::getPassword, req.getPassword());
        TouristDO touristDO = baseMapper.selectOne(wrapper);
        if (touristDO == null) {
            throw new ClientException("账号不存在，请前往注册");
        }
        return touristDO;
    }


}
